#include "commonpage.h"
#include "ui_commonpage.h"
#include<QDebug>

CommonPage::CommonPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CommonPage)
{
    ui->setupUi(this);
    ui->pageMusicList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);//去掉水平滚动条
    connect(ui->playAllBtn,&QPushButton::clicked,this,[=](){
        emit playAll(pageType);
    });
    connect(ui->pageMusicList,&QListWidget::doubleClicked,this,[=](const QModelIndex &index){
        emit playMusicByIndex(this,index.row());
    });
}

CommonPage::~CommonPage()
{
    delete ui;
}

void CommonPage::setCommonPageUi(const QString &title, const QString &imagePath)
{
    ui->pageTittle->setText(title);
    ui->musicImageLabel->setPixmap(QPixmap(imagePath));

    ui->musicImageLabel->setScaledContents(true);//图片内容根据QLabel尺寸就行缩放
}

void CommonPage::setMuiscPageType(PageType pageType)
{
    this->pageType=pageType;
}

void CommonPage::addMusicToMusicPage(MusicList &musiclist)
{
    musicListOfPage.clear();
    for(auto&music:musiclist)
    {
        switch(pageType)
        {
        case LIKE_PAGE:
            if(music.getIsLike())
            {
                musicListOfPage.push_back(music.getMusicId());
            }
                break;
        case LOCAL_PAGE:
            musicListOfPage.push_back(music.getMusicId());
            break;
        case RECENT_PAGE:
            if(music.getIsHistory())
            {
                musicListOfPage.push_back(music.getMusicId());
            }
            break;
        default:
            qDebug()<<"版本尚不支持";
            break;
        }
    }
}

void CommonPage::reFresh(MusicList &musiclist)
{
    ui->pageMusicList->clear();
    addMusicToMusicPage(musiclist);
    for(auto musicId:musicListOfPage)
    {
        auto it=musiclist.findMusicById(musicId);
        if(it!=musiclist.end())
        {
            ListItemBox* item=new ListItemBox(this);
            item->setMusicName(it->getMusicName());
            item->setMusicSinger(it->getMusicSinger());
            item->setMusicAlbum(it->getMusicAlbum());
            item->setLikeMusic(it->getIsLike());
            QListWidgetItem* listWidgetItem=new QListWidgetItem(ui->pageMusicList);
            qDebug()<<listWidgetItem->sizeHint();//listWigetItem创建出来原始的大小
            listWidgetItem->setSizeHint(QSize(item->width(),item->height()));//根据item的宽高设置
            qDebug()<<listWidgetItem->sizeHint();
            ui->pageMusicList->setItemWidget(listWidgetItem,item);

            connect(item,&ListItemBox::setIsLike,this,[=](bool isLike){
                emit updataLikeMusic(isLike,it->getMusicId());
            });
        }
        else continue;
    }
    // repaint()会⽴即执⾏paintEvent()，不会等待事件队列的处理
    // update（）将⼀个paintEvent事件添加到事件队列中，等待稍后执⾏，即不会⽴即执⾏paintEvent。
    repaint();
}

void CommonPage::addMusicToPlayer(MusicList &musicList, QMediaPlaylist *playList)
{
    for(auto&music:musicList)
    {
        switch(pageType)
        {
        case LIKE_PAGE:
            if(music.getIsLike())
            {
                playList->addMedia(music.getMusicUrl());
            }
            break;
        case LOCAL_PAGE:
            playList->addMedia(music.getMusicUrl());
            break;
        case RECENT_PAGE:
            if(music.getIsHistory())
            {
                playList->addMedia(music.getMusicUrl());
            }
            break;
        default:
            qDebug()<<"暂不支持";
        }
    }
}

QString CommonPage::getMusicIdByIndex(int index)
{
    if(index>=musicListOfPage.size())
    {
        qDebug()<<"查无此曲";
        return "";
    }
    return musicListOfPage[index];
}

void CommonPage::setImageLabel(QPixmap pixmap)
{
    ui->musicImageLabel->setPixmap(pixmap);
    ui->musicImageLabel->setScaledContents(true);
}


